package com.xueqiu.movieplaymanagersystem.controller.admin;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import com.xueqiu.movieplaymanagersystem.controller.admin.publics.PublicController;
import com.xueqiu.movieplaymanagersystem.model.JsonResult;
import com.xueqiu.movieplaymanagersystem.entity.Type;
import com.xueqiu.movieplaymanagersystem.service.PublicService;

/**
 * @author xueqiu
 */
@Controller
@RequestMapping("/admin")
public class TypesController {
    private PublicService<Type> publicService;

    @Autowired
    public void setPublicService(
            @Qualifier("typesService")
            PublicService<Type> publicService
    ) {
        this.publicService = publicService;
    }

    /**
     * 新增类型
     * @param type 类型名称
     * @return 新增成功与否
     */
    @GetMapping("/addOneType/{type}")
    @ResponseBody
    public JsonResult<Type> save(
            @PathVariable(name = "type")
            String type
    ) {
        JsonResult<Type> jsonResult = PublicController.isTrueString(type);
        if (jsonResult != null) {
            return jsonResult;
        }
        jsonResult = new JsonResult<>();
        Type originData = new Type();
        originData.setTypeName(type);
        return PublicController.save(publicService, originData, jsonResult);
    }

    /**
     * 编辑类型
     * @param id 类型id
     * @param typeName 类型名称
     * @return 更新成功与否
     */
    @GetMapping("/editOneType/{id}/{typeName}")
    @ResponseBody
    public JsonResult<String> edit(
            @PathVariable(name = "id")
            String id,
            @PathVariable(name = "typeName")
            String typeName
    ) {
        return PublicController.edit(id, typeName, publicService);
    }

    /**
     * 删除单个类型；根据类型id
     * @param id 类型id
     * @return 删除成功与否
     */
    @GetMapping("/deleteType/{id}")
    @ResponseBody
    public JsonResult<String> deleteById(
            @PathVariable(name = "id")
            String id
    ) {
        return PublicController.deleteById(id, publicService);
    }

    /**
     * 批量删除类型；根据类型名称
     * @param types 类型名称
     * @return 删除成功与否
     */
    @PostMapping("/batchDeleteType")
    @ResponseBody
    public JsonResult<String> deleteByIdBatch(
            @RequestParam(name = "types[]")
            String[] types
    ) {
        return PublicController.deleteByIdBatch(types, publicService);
    }

    /**
     * 单页查询类型、分页查询类型
     * @param type 关键词
     * @param page 当前页
     * @param limit 每页条数
     * @return 单页查询数据或者多页查询中的某一页数据；可能为empty
     */
    @RequestMapping(value = "/types", method = RequestMethod.GET)
    @ResponseBody
    public JsonResult<Type> findAll(
            @RequestParam(required = false, name = "type")
            String type,
            @RequestParam(name = "page")
            int page,
            @RequestParam(name = "limit")
            int limit
    ) {
        return PublicController.findAll(type, page, limit, publicService);
    }
}
